package com.zhongxi.guoyuan.adapter.seller;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.seller.SellerBeautifulOrderData;
import com.zhongxi.guoyuan.entity.seller.SellerOrderInfoResult;
import com.zhongxi.guoyuan.entity.user.UserData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;

import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2016/12/21.
 */

public class SellerBeautifulAdapter extends BaseAdapter {

    private Context context;
    private List<SellerBeautifulOrderData> list;
    private JSONObject obj;
    private int type;
    private int mExpandedMenuPos = -1;
    private SellerOrderInfoResult infoResult;
    private Handler handler;

    public SellerBeautifulAdapter(Context context, List<SellerBeautifulOrderData> orderData, Handler handler) {
        this.context = context;
        this.list = orderData;
        this.handler = handler;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getmExpandedMenuPos() {
        return mExpandedMenuPos;
    }

    public void setmExpandedMenuPos(int mExpandedMenuPos, SellerOrderInfoResult infoResult) {
        this.mExpandedMenuPos = mExpandedMenuPos;
        this.infoResult = infoResult;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_seller_beautiful_order, parent, false);
            holder.ivImg = (ImageView) convertView.findViewById(R.id.ivServiceImg);
            holder.ivCall = (ImageView) convertView.findViewById(R.id.ivCall);
            holder.tvServiceName = (TextView) convertView.findViewById(R.id.tvServiceName);
            holder.tvOrderTime = (TextView) convertView.findViewById(R.id.tvServiceTime);
            holder.tvUserName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvUserPhone = (TextView) convertView.findViewById(R.id.tvClientPhone);
            holder.tvOrderNum = (TextView) convertView.findViewById(R.id.tvOrderNum);
            holder.tvOper = (TextView) convertView.findViewById(R.id.tvOper);

            holder.tvInfoNum = (TextView) convertView.findViewById(R.id.tvOrderInfo);
            holder.tvInfoName = (TextView) convertView.findViewById(R.id.tvOrderName);
            holder.tvOrign = (TextView) convertView.findViewById(R.id.tvOrign);
            holder.tvTruePrice = (TextView) convertView.findViewById(R.id.tvTruePrice);
            holder.tvInfoReward = (TextView) convertView.findViewById(R.id.tvForword);
            holder.tvInfoTime = (TextView) convertView.findViewById(R.id.tvSubTime);
            holder.tvInfoUserName = (TextView) convertView.findViewById(R.id.tvOrderStoreName);
            holder.tvInfoUserPhone = (TextView) convertView.findViewById(R.id.tvOrderStorePhone);
            holder.tvInfoUserAddress = (TextView) convertView.findViewById(R.id.tvInfoUserAddress);
            holder.linearComplain = (LinearLayout) convertView.findViewById(R.id.linearComplain);

            holder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);

            holder.list_menu = (View) convertView.findViewById(R.id.list_menu);
            holder.tvType = (TextView) convertView.findViewById(R.id.tvType);
            holder.tvTips = (TextView) convertView.findViewById(R.id.tvTips);
            holder.tvIdentify = (TextView) convertView.findViewById(R.id.tvIdentify);
            holder.tvMsg = (TextView) convertView.findViewById(R.id.tvMsg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final int pos = position;
        switch (type) {
            case 1:
                holder.tvOper.setVisibility(View.VISIBLE);
                holder.linearComplain.setVisibility(View.GONE);
                holder.tvOper.setText("受理");
                holder.tvOper.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Message msg = Message.obtain();
                        msg.what = 0;
                        msg.obj = pos;
                        handler.sendMessage(msg);
                    }
                });
                break;
            case 2:
                holder.tvOper.setVisibility(View.VISIBLE);
                holder.linearComplain.setVisibility(View.GONE);
                holder.tvOper.setText("确认操作");
                holder.tvOper.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new AlertDialog.Builder(context).setTitle("您确认该项目已经护理结束吗？")
                                .setPositiveButton("确定已结束", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Message msg = Message.obtain();
                                        msg.what = 1;
                                        msg.obj = pos;
                                        handler.sendMessage(msg);
                                        dialog.dismiss();
                                    }
                                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).show();
                    }
                });
                break;
            case 3:
                holder.tvOper.setVisibility(View.GONE);
                holder.linearComplain.setVisibility(View.VISIBLE);
                break;
        }
        SellerBeautifulOrderData data = list.get(position);

        if(null!=data.getSstype()&&!"".equals(data.getSstype())){
                holder.list_menu.setVisibility(position == mExpandedMenuPos ? View.VISIBLE:View.GONE);
            if("1".equals(data.getSstype())){
                holder.tvType.setText("上门服务");
                holder.tvTips.setText("客户基本信息");
            }else{
                holder.tvType.setText("到店服务");
                holder.tvTips.setText("美容院基本信息");
            }
        }
        if(null!=data.getId()&&!"".equals(data.getId())){
            holder.tvInfoNum.setText("订单号："+data.getId());
        }else{
            holder.tvInfoNum.setText("订单号：*");
        }
        if (null != data.getServiceimg() && !"".equals(data.getServiceimg())) {
            Glide.with(context).load(Url.IMG_URL+data.getServiceimg()).placeholder(R.mipmap.iv_eg).transform(new GlideRoundTransform(context, 20)).into(holder.ivImg);
        }
        if (null != data.getServicename() && !"".equals(data.getServicename())) {
            holder.tvServiceName.setText("项目：" + data.getServicename());
            holder.tvInfoName.setText("项目：" + data.getServicename());
        } else {
            holder.tvServiceName.setText("项目：");
            holder.tvInfoName.setText("项目：");
        }
        if (null != data.getCtime() && !"".equals(data.getCtime())) {
            holder.tvOrderTime.setText("下单时间：" + data.getCtime());
        } else {
            holder.tvOrderTime.setText("下单时间：");
        }
        String money = "实付：";
        if (null != data.getId() && !"".equals(data.getId())) {
            holder.tvUserName.setText("订单ID：" + data.getId());
        } else {
            holder.tvUserName.setText("订单ID：*");
        }
//        if (null != data.getPayinnerprice() && !"".equals(data.getPayinnerprice())) {
//            money += data.getPayinnerprice() + "美币";
//        } else {
//            money += "0美币";
//        }
        if (null != data.getPay() && !"".equals(data.getPay())) {
            money += data.getPay() + "元";
        } else {
            money += "0元";
        }
        holder.tvOrderNum.setText(money);
        holder.tvTruePrice.setText(money);
        String orign = "原价：";
//        if (null != data.getShouldinnerprice() && !"".equals(data.getShouldinnerprice())) {
//            orign += data.getShouldinnerprice() + "美币";
//        } else {
//            orign += "0美币";
//        }
        if (null != data.getShouldprice() && !"".equals(data.getShouldprice())) {
            orign += data.getShouldprice() + "元";
        } else {
            orign += "0元";
        }
        holder.tvOrign.setText(orign);
        if (null != obj && !"".equals(obj)) {
            try {
                Gson gson = new Gson();
                if (null != data.getUserid() && !"".equals(data.getUserid())) {
                    String userInfo = obj.getJSONObject("user").getString(data.getUserid());
                    UserData userData = gson.fromJson(userInfo, UserData.class);
                    if (null != userData.getMphone() && !"".equals(userData.getMphone())) {
                        holder.tvUserPhone.setText("手机号：" + userData.getMphone());
                    } else {
                        holder.tvUserPhone.setText("手机号：");
                    }
                    if(null!=userData.getIsprivilege()&&!"".equals(userData.getIsprivilege())){
                        if("1".equals(userData.getIsprivilege())){
                            if (null != data.getPay() && !"".equals(data.getPay())) {
                                holder.tvInfoReward.setText("返银："+new BigDecimal(data.getPay()).multiply(new BigDecimal(data.getBacksilverratio())).intValue()/100+"银币");
                            }
                            holder.tvIdentify.setText("权益会员");
                        }else{
                            holder.tvInfoReward.setText("返银：0银币");
                            holder.tvIdentify.setText("普通会员");
                        }
                    }else{
                        holder.tvInfoReward.setText("返银：0银币");
                        holder.tvIdentify.setText("普通会员");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (null != infoResult && !"".equals(infoResult)) {
            if(null!=infoResult.getComplaints()&&!"".equals(infoResult.getComplaints())){
//                holder.linearComplain.setVisibility(View.VISIBLE);
                if(null!=infoResult.getComplaints().getContent())
                holder.tvContent.setText(infoResult.getComplaints().getContent());
            }else{
                holder.tvContent.setText("");
            }
            if (null != infoResult.getSubscribeInfo() && !"".equals(infoResult.getSubscribeInfo())) {
                SellerOrderInfoResult.SellerOrderSubscribeInfo subscribeInfo = infoResult.getSubscribeInfo();
                if (null != subscribeInfo.getSubscribetime() && !"".equals(subscribeInfo.getSubscribetime())) {
                    holder.tvInfoTime.setText(subscribeInfo.getSubscribetime());
                } else {
                    holder.tvInfoTime.setText("");
                }
                /*if ("1".equals(subscribeInfo.getSubsctibetype())) {
                    holder.linearAddress.setVisibility(View.VISIBLE);
                } else {
                    holder.linearAddress.setVisibility(View.GONE);
                }*/
            }
            if (null != infoResult.getExtInfo() && !"".equals(infoResult.getExtInfo())) {
                final SellerOrderInfoResult.SellerExtInfo extInfo = infoResult.getExtInfo();
                if(null!=data.getSstype()&&"1".equals(data.getSstype())){
                    if(null!=extInfo.getMphone()&&!"".equals(extInfo.getMphone())){
                        holder.tvInfoUserPhone.setText(extInfo.getMphone());
                        holder.ivCall.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                try{
                                    Intent intent = new Intent(Intent.ACTION_DIAL);
                                    Uri data = Uri.parse("tel:" + extInfo.getMphone());
                                    intent.setData(data);
                                    context.startActivity(intent);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                        });
                    }else{
                        holder.tvInfoUserPhone.setText("");
                    }
                    holder.tvMsg.setVisibility(View.VISIBLE);
                    if(null!=extInfo.getMsg()&&!"".equals(extInfo.getMsg())){
                        holder.tvMsg.setText("预留信息："+extInfo.getMsg());
                    }else{
                        holder.tvMsg.setText("预留信息：");
                    }
                }else{
                    if(null!=extInfo.getPhonenum()&&!"".equals(extInfo.getPhonenum())){
                        holder.tvInfoUserPhone.setText(extInfo.getPhonenum());
                        holder.ivCall.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                try{
                                    Intent intent = new Intent(Intent.ACTION_DIAL);
                                    Uri data = Uri.parse("tel:" + extInfo.getPhonenum());
                                    intent.setData(data);
                                    context.startActivity(intent);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                        });
                    }else{
                        holder.tvInfoUserPhone.setText("");
                    }
                    holder.tvMsg.setVisibility(View.GONE);
                }
                if(null!=extInfo.getAddress()&&!"".equals(extInfo.getAddress())){
                    holder.tvInfoUserAddress.setText(extInfo.getAddress());
                }else{
                    holder.tvInfoUserAddress.setText("");
                }

                if(null!=data.getSstype()&&!"".equals(data.getSstype())) {
                    if ("1".equals(data.getSstype())) {
                        if (null != data.getUserid() && !"".equals(data.getUserid())) {
                            holder.tvInfoUserName.setText(data.getUserid());
                        } else {
                            holder.tvInfoUserName.setText("*");
                        }
                    } else {
                        if(null!=extInfo.getName()&&!"".equals(extInfo.getName())){
                            holder.tvInfoUserName.setText(extInfo.getName());
                        }else{
                            holder.tvInfoUserName.setText("");
                        }
                    }
                }
            }
        }

        return convertView;
    }

    public void setJSONObject(JSONObject obj) {
        this.obj = obj;
    }

    class ViewHolder {
        private ImageView ivImg;
        private TextView tvServiceName;
        private TextView tvOrderTime;
        private TextView tvUserName;
        private TextView tvUserPhone;
        private TextView tvOrderNum;
        private TextView tvOper;

        private TextView tvInfoNum;
        private TextView tvInfoName;
        private TextView tvOrign;
        private TextView tvTruePrice;
        private TextView tvInfoReward;
        private TextView tvInfoTime;
        private TextView tvInfoUserName;
        private TextView tvInfoUserPhone;
        private TextView tvInfoUserAddress;
        private ImageView ivCall;
//        private LinearLayout linearAddress;
        private LinearLayout linearComplain;

        private View list_menu;
        private TextView tvContent;
        private TextView tvType;
        private TextView tvTips;
        private TextView tvIdentify;
        private TextView tvMsg;
    }
}
